<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>修改密码</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../css/oksub.css">
</head>
<body>
<div class="ok-body">
    <!--form表单-->
    <form class="layui-form layui-form-pane ok-form">
        <div class="layui-form-item">
            <label class="layui-form-label">旧密码</label>
            <div class="layui-input-block">
                <input  type="password" name="oldPwd" placeholder="请输入旧密码" class="layui-input" lay-verify="required|password" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-block">
                <input type="password" name="newPwd" lay-verify="required|password" placeholder="请输入新密码" autocomplete="off" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="edit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<!--js逻辑-->
<script src="../lib/layui/layui.js"></script>
<script>
    layui.use(["element", "form", "laydate", "okLayer", "okUtils","okMd5","jquery"], function () {
        let form = layui.form;
        let laydate = layui.laydate;
        let okLayer = layui.okLayer;
        let okUtils = layui.okUtils;
        let okMd5 = layui.okMd5;
        let $ = layui.jquery;

        laydate.render({elem: "#birthday", type: "datetime"});

        form.verify({
            birthdayVerify: [/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))(\s(([01]\d{1})|(2[0123])):([0-5]\d):([0-5]\d))?$/, '日期格式不正确']
        });

        /**
         * 数据校验
         */
        form.verify({
            password: [/^[\S]{6,18}$/, "密码必须6到18位，且不能出现空格"],
            captcha: function (val) {
                if (verifyCode.validate(val) != "true") {
                    return verifyCode.validate(val)
                }
            }
        });

        // 获取contentText
        const ctx = function () {
            let pathName = document.location.pathname;
            let index = pathName.substr(1).indexOf("/");
            let result = pathName.substr(0, index + 1);
            return result;
        }();

        var admin = JSON.parse(window.sessionStorage.getItem("user"));

        if(admin==null){
            okLayer.yellowSighMsg("登陆时间超时，请重新登录！", function () {
                window.sessionStorage.clear();
                top.window.location = ctx+"/pages/admin/pages/adminLogin.html";
            });
        }

        form.on("submit(edit)", function (data) {
            var initDate=data.field;
            console.log(initDate,"上传数据",admin.adminid)
            okLayer.confirm("确定要修改密码吗？新密码为："+initDate.newPwd, function () {
                $.ajax({
                    url:ctx+'/admin/updateadminPwd',
                    type:'POST',
                    data:{"adminid": admin.adminid,"oldPwd":okMd5(initDate.oldPwd),"newPwd":okMd5(initDate.newPwd)},
                    dataType:'json',
                    error:function(){
                        okLayer.redCrossMsg("服务器错误")
                    },
                    success:function(data){
                        if (data.code == 200) {
                            // 业务正常
                            okLayer.greenTickMsg(data.msg,function () {
                                if(window != top){
                                    top.window.sessionStorage.clear();
                                    top.window.location = ctx+"/pages/admin/pages/adminLogin.html";
                                }
                            });
                        } else {
                            // 业务异常
                            okLayer.yellowSighMsg(data.msg);
                        }
                    }
                });
            })
            return false;
        });
    });
</script>
</body>
</html>
